package com.refactoring.demo.chapter01;


import java.util.Enumeration;
import java.util.Vector;

/**
 * 顾客
 *
 * @作者：刘富国
 * @创建时间：2017/12/20 17:10
 */
public class Customer {
    /**
     * 顾客名字
     */
    private String _name;
    /**
     * 顾客租赁向量
     */
    private Vector _rentals = new Vector();

    public Customer(String _name) {
        this._name = _name;
    }

    public void addRental(Rental arg) {
        _rentals.addElement(arg);
    }

    public String getName() {
        return _name;
    }


    public String statement() {
        String result = "租赁记录：" + getName() + "\n";
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += "\t" + each._movie.get_movie().getTitle() + "\t" + each.getCharge() + "\n";
        }

        return result + "需缴费" + getTotalCharge() + "\n" + "你赚了" + getFreqRenterPoints() + "积分";

    }

    /**
     * 计算总费用
     */
    public double getTotalCharge() {
        double totalCharge = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            totalCharge += each.getCharge();
        }
        return totalCharge;
    }

    /**
     * 计算常客积分点
     */
    public int getFreqRenterPoints() {
        int freqRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            freqRenterPoints += each.getFreqRenterPoints();
        }
        return freqRenterPoints;
    }

        public String htmlStatement() {
        Enumeration elements = _rentals.elements();
        String result = "<h1>租赁给： <em>" + getName() + "</em><h1><p>\n";
        while (elements.hasMoreElements()) {
            Rental each = (Rental) elements.nextElement();
            result += each._movie.get_movie().getTitle() + ": " + each.getCharge() + "<br>\n";

        }

        result += "<p>需缴费<em>" + getTotalCharge() + "</em><p>\n";
        result += "租赁这些电影您获得了<en>" + getFreqRenterPoints() + "</em>积分<p>";
        return result;

    }


}
